package com.mentaleye.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.mentaleye.bean.CompanyBean;
import com.mentaleye.biz.CompanyBiz;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Controller
@RequestMapping("/companyController")
public class CompanyController {

	@Autowired
	private DictionaryBiz dictionaryBiz;

	@Autowired
	private CompanyBiz companyBiz;

	@RequestMapping("/toListPage")
	public ModelAndView toListPage() {
		ModelAndView view = new ModelAndView("em/company/companyList");
		view.addObject("companyTypeList",
				this.dictionaryBiz.getListByParent(DictionaryEnums.COMPANY_TYPE.getCode()));
		return view;
	}

	@RequestMapping("/toInsertPage")
	public ModelAndView toInsertPage() {
		ModelAndView view = new ModelAndView("em/company/companyAdd");
		view.addObject("companyTypeList",
				this.dictionaryBiz.getListByParent(DictionaryEnums.COMPANY_TYPE.getCode()));
		return view;
	}
	
	@RequestMapping("/toEditPage")
	public ModelAndView toEditPage(@RequestParam int companyId) {
		ModelAndView view = new ModelAndView("em/company/companyEdit");
		view.addObject("companyTypeList",
				this.dictionaryBiz.getListByParent(DictionaryEnums.COMPANY_TYPE.getCode()));
		view.addObject("company", this.companyBiz.getMapById(companyId));
		return view;
	}

	@RequestMapping("/insertData")
	@ResponseBody
	public String insertData(CompanyBean dataBean) {
        CommonResult result = this.companyBiz.insertData(dataBean);
		return JSON.toJSONString(result);
	}

	@RequestMapping("/updateData")
	@ResponseBody
	public String updateData(CompanyBean dataBean) {
		CommonResult result = this.companyBiz.updateData(dataBean);
		return JSON.toJSONString(result);
	}

	@RequestMapping("/deleteData")
	@ResponseBody
	public String deleteData(@RequestBody List<Integer> idList) {
		CommonResult result = this.companyBiz.deleteByKeyList(idList);
		return JSON.toJSONString(result);
	}

	@RequestMapping("/getList")
	@ResponseBody
	public String getList(CommonPage page) {
        this.companyBiz.getListByPage(page);
        return page.toSHowPage();
	}
}
